//
//  ShibaoViewController.m
//  OneWordNew
//
//  Created by qianfeng on 15-7-2.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "ShibaoViewController.h"
#import "NewModel.h"
#import "HotCell.h"
#import "AFNetworking.h"
#import "MyWebViewController.h"
#import "JHRefresh.h"
@interface ShibaoViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_dataArr;
    UITableView *_tableView;
    AFHTTPRequestOperationManager *_manager;
    BOOL _isRefresh;
    BOOL _isLoadMore;
    NSInteger _currenter;
    NSMutableArray *_imageArr;
    
}
@property(nonatomic) BOOL isRefresh;
@property(nonatomic) BOOL isLoadMore;
@property(nonatomic) NSInteger currenter;
@property(nonatomic,retain) NSMutableArray *dataArr;
@property(nonatomic,retain) UITableView *tableView;

@end

@implementation ShibaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self firstDownLoad];
    [self creatRefreshView];
    
}
- (void)firstDownLoad{
    _isLoadMore=NO;
    _isRefresh=NO;
    _currenter=0;
    [self loadDataPage:_currenter isRefresh:NO];

}
- (void)creatUI{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"top"] forBarMetrics:UIBarMetricsDefault];
    self.automaticallyAdjustsScrollViewInsets=NO;
    _imageArr=[[NSMutableArray alloc] initWithObjects:@"hottestOne",@"hottestTwo",@"hottestThree", nil];
    self.dataArr=[[NSMutableArray alloc] init];
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-49-64) style:UITableViewStylePlain];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [self.tableView registerNib:[UINib nibWithNibName:@"HotCell" bundle:nil] forCellReuseIdentifier:@"HotCell"];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    [self.view addSubview:self.tableView];
    
}
- (void)loadDataPage:(NSInteger)page isRefresh:(BOOL)isRefresh{
    __weak typeof (self)weakSelf=self;
    NSString *url=[NSString stringWithFormat:kURLHot,weakSelf.currenter];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // NSLog(@"%@",responseObject);
        if (responseObject) {
            if (_isRefresh) {
                [weakSelf.dataArr removeAllObjects];
            }
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *data=dict[@"data"];
            for (NSDictionary *sub in data) {
                NewModel *model=[[NewModel alloc] init];
                [model setValuesForKeysWithDictionary:sub];
                [weakSelf.dataArr addObject:model];
            }
            [weakSelf.tableView reloadData];
            [weakSelf endRefreshing];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        [weakSelf endRefreshing];
    }];
    
    
}
- (void)creatRefreshView{
    __weak typeof(self)weakSelf=self;
    [weakSelf.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (_isRefresh) {
            return ;
        }
        weakSelf.isRefresh=YES;
        weakSelf.currenter=0;
        [weakSelf loadDataPage:_currenter isRefresh:YES  ];
        
    }];
    [weakSelf.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (_isLoadMore) {
            return ;
        }
        weakSelf.isLoadMore=YES;
        weakSelf.currenter+=10;
        [weakSelf loadDataPage:_currenter isRefresh:YES];
    }];
    
    
}
- (void)endRefreshing{
    if (self.isRefresh) {
        self.isRefresh=NO;
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }
    if (self.isLoadMore) {
        self.isLoadMore=NO;
        [self.tableView footerEndRefreshing];
    }
    
    
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotCell *cell=[self.tableView dequeueReusableCellWithIdentifier:@"HotCell" forIndexPath:indexPath];
    NewModel *model=self.dataArr[indexPath.row];
    [cell showDataWithModel:model];
    if (indexPath.row<3) {
        cell.topImage.hidden = NO;
        cell.topImage.image=[UIImage imageNamed:_imageArr[indexPath.row]];
    } else {
        cell.topImage.hidden = YES;
    }
    return cell;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 105;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyWebViewController *web=[[MyWebViewController alloc] init];
    NewModel *model=self.dataArr[indexPath.row];
    web.webUrl=model.absolute_url;
    web.idd=model.id;
    
    //    self.navigationController.navigationBarHidden=YES;
    // [self.tabBarController.tabBar setHidden:YES];
    [self.navigationController pushViewController:web animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden=NO;
    
}/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
